<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="districtFormRules" :model="districtForm" label-position="left" label-width="140px">
      <el-form-item label="简称" prop="title">
        <el-input v-model="districtForm.title" />
      </el-form-item>
      <el-form-item label="全称" prop="fullname">
        <el-input v-model="districtForm.fullname" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="districtForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" v-loading="loading">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="districtForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="districtForm.status.value === 5 && districtForm.ID > 0" type="danger" @click="handleDelete(districtForm)">
        删除
      </el-button>
      <el-button type="default" @click="handleImport(districtForm)">
        导入
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveDistrict, removeDistrictByID, getDistrictDetail } from '@/api/geography/district'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      loading:false,
      tableKey: 0,
      districtForm: {
        fullname: '',
        title: '',
        parentDistrictID:0,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      employeeOptions:[],
      districtFormRules: []
    }
  },
  created(options) {
    let parentDistrictID = this.$route.query && this.$route.query.parentDistrictID
    console.log(parentDistrictID)
    if(parentDistrictID != undefined){
      this.districtForm.parentDistrictID = parentDistrictID
    }
    let districtID = this.$route.query && this.$route.query.districtID
    if (districtID !== undefined) {
      districtID = parseInt(districtID)
      if (!isNaN(districtID)) {
        this.listLoading = true
        const formData = {
          'districtID': districtID
        }
        getDistrictDetail(formData).then(response => {
          if (response.status === 200) {
            this.districtForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(districtID)
  },
  methods: {
    loadEmployeeList() {
      getEmployeeUsable().then(response => {
        if (response.status === 200) {
          this.employeeOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'district' })
    },
    handleImport:function(row){
      if(row != undefined){
          this.$router.push({ path: 'district-import', query: { 'parentDistrictID': row.ID }})
      }else{
        if(this.districtForm.districtID != undefined){
            this.$router.push({ path: 'district-import', query: { 'parentDistrictID': this.districtForm.districtID }})
        }else{
          this.$router.push({ path: 'district-import' })
        }
      }
    },
    draftData() {
      this.listLoading = true
      const _districtForm = this.districtForm
      _districtForm.status = 1
      if (_districtForm.ID > 0) {
        _districtForm.districtID = _districtForm.ID
      }
      saveDistrict(_districtForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'district',query:{"districtID":this.districtForm.parentDistrictID} })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _districtForm = this.districtForm
      _districtForm.status = 6
      if (_districtForm.ID > 0) {
        _districtForm.districtID = _districtForm.ID
      }
          this.loading = true;
      saveDistrict(_districtForm).then(response => {
          this.loading = false;
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'district',query:{"districtID":this.districtForm.parentDistrictID} })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'districtID': row.ID
      }
      removeDistrictByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'district' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
